package servlet;


import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bean.*;
import DAO.*;
import service.*;
/**
 * 
 * @author Administrator
 *
 */
public class LoginServlet extends HttpServlet {

	
	public LoginServlet() {
		super();
	}

	public void destroy() {
		super.destroy(); 
	}

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

			this.doPost(request, response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			request.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=utf-8");
			//获取login.jsp中用户登录表单中提交的username
			String id = request.getParameter("username");
			//获取login.jsp中用户登录表单中提交的password
			String password = request.getParameter("password");
			
			//实例化一个user，将前台获取的username,password赋值给user实例的属性
			Userbean user = new Userbean();
			user.setId(id);
			user.setPassword(password);
			
			////调用DAO，进行用户登录
			IUserDao dao = DaoFactory.getUserDao();
			if(dao.queryById(user)!=null){
				//登录成功则请求转发至首页
				request.getRequestDispatcher("main1.jsp").forward(request, response);
			}else{
				//登录失败则重定向至登录页
				response.sendRedirect("login.jsp");
			}
		
	}

	public void init() throws ServletException {
	}

}
